﻿using MLSMooc.BO;
using MLSMooc.Service.Interface.Discussion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MLSMooc.Helper;
using Dapper;

namespace MLSMooc.Service.Implement.Discussion
{
    public class DiscussionServiceImpl : IDiscussionService
    {
        private static DiscussionServiceImpl instance = new DiscussionServiceImpl();
        private DiscussionServiceImpl() { }
        public static DiscussionServiceImpl Getinstance() => instance;

        //新增评论
        public async Task<int> InsertDiscussion(DiscussionBO data)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder("insert into Mooc_Course_Discussion values(@CourseId,@AdminId,@Content,getdate(),getdate(),0)");
                var res = await conn.ExecuteAsync(sql.ToString(), new { CourseId = data.CourseId, AdminId = data.AdminId, Content = data.Content });
                return res;
            }
        }

        //删除评论
        public async Task<int> DeleteDiscussion(int id)
        {
            using (SqlConnection conn=new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"Update Mooc_Course_Discussion set isDelete=1 where Id=@id");
                var res = await conn.ExecuteAsync(sql.ToString(), new { id = id });
                return res;
            }
        }

        //删除评论回复
        public async Task<int> DeleteDiscussionReply(int id)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"Update Mooc_Course_DiscussionReply set isDelete=1 where Id=@id");
                var res = await conn.ExecuteAsync(sql.ToString(), new { id = id });
                return res;
            }
        }


        //回复讨论
        public async Task<int> ReplyDiscussion(DiscussionReplyBO data)
        {
            using (SqlConnection conn=new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder("insert into Mooc_Course_DiscussionReply values(@DiscussionId,@AdminId,@Content,@ReplyAdminId,getdate(),getdate(),0)");
                var res = await conn.ExecuteAsync(sql.ToString(), new { DiscussionId = data.DiscussionId, AdminId = data.AdminId, Content = data.Content, ReplyAdminId = data.ReplyAdminId });
                return res;
            }
        }

        //更新课程讨论数量
        public async Task<int> UpCourseDisCount(int id)
        {
            using (SqlConnection conn=new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder("Update Mooc_Course set Course_CommentNumber=(select Count(*) from Mooc_Course_Discussion where isDelete=0 and CourseId=@CId) where Id=@Id");
                var res = await conn.ExecuteAsync(sql.ToString(),new { CId=id,Id=id});
                return res;
            }
        }
    }
}
